﻿Imports System.Windows.Forms
Imports System.ComponentModel

Public Class CudaWatcherNotifyIcon
    Inherits ApplicationContext

    Private WithEvents appIcon As NotifyIcon
    Private WithEvents mainMenu As ContextMenuStrip
    Private WithEvents mnuDisplaySettings As ToolStripMenuItem
    Private WithEvents mnuStartStop As ToolStripMenuItem
    Private WithEvents mnuSep1 As ToolStripSeparator
    Private WithEvents mnuExit As ToolStripMenuItem
    Private WithEvents cudaController As CudaController
    Private WithEvents cudaWatcherWorker As BackgroundWorker


    Public Sub New()
        mnuDisplaySettings = New ToolStripMenuItem("Settings")
        mnuStartStop = New ToolStripMenuItem("Stop")
        mnuSep1 = New ToolStripSeparator
        mnuExit = New ToolStripMenuItem("Exit")
        mainMenu = New ContextMenuStrip
        mainMenu.Items.AddRange(New ToolStripItem() {mnuDisplaySettings, mnuStartStop, mnuSep1, mnuExit})

        'Initialize the tray
        appIcon = New NotifyIcon
        appIcon.Icon = My.Resources.LogoGray
        appIcon.ContextMenuStrip = mainMenu
        appIcon.Text = "CUDA Watcher"

        'Display
        appIcon.Visible = True
        cudaWatcherWorker = New BackgroundWorker
        cudaWatcherWorker.WorkerSupportsCancellation = True
        cudaController = New CudaController
        cudaWatcherWorker.RunWorkerAsync()
    End Sub

    Private Sub cudaWatcherWorker_DoWork(ByVal sender As System.Object, _
                 ByVal e As System.ComponentModel.DoWorkEventArgs) _
                 Handles cudaWatcherWorker.DoWork
        If e.Cancel = True Then
            KillCUDAProcess()
            Exit Sub
        End If
        cudaController.WatchProcesses()
    End Sub

    Public Sub ExitApplication()
        'Perform any clean-up here
        'Then exit the application
        KillCUDAProcess()
        Application.Exit()
    End Sub

    Private Sub KillCUDAProcess()
        cudaController.KillCudaProcess()
    End Sub

#Region " Event handlers "

    Private Sub AppContext_ThreadExit(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles Me.ThreadExit
        'Guarantees that the icon will not linger.
        appIcon.Visible = False
        cudaWatcherWorker.CancelAsync()
    End Sub

    Private Sub mnuStartStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles mnuStartStop.Click
        If cudaController.cudaRunning Then
            cudaController.KillCudaProcess()
        Else
            cudaWatcherWorker.RunWorkerAsync()
        End If
    End Sub

    Private Sub mnuDisplaySettings_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles mnuDisplaySettings.Click
        Dim settingsDialog As New CudaWatcherSettings
        settingsDialog.ShowDialog()
    End Sub

    Private Sub mnuExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles mnuExit.Click
        ExitApplication()
    End Sub

    Private Sub Tray_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles appIcon.DoubleClick
        'ShowDialog()
    End Sub

    Private Sub ControllerMessage(message As String) Handles cudaController.SendMessage
        appIcon.ShowBalloonTip(2000, "Cuda Watcher", message, ToolTipIcon.Info)
        appIcon.Text = message
    End Sub

    Private Sub CudaRunningIcon() Handles cudaController.CudaRunningNow
        appIcon.Icon = My.Resources.LogoGreen
        mnuStartStop.Text = "Stop"
    End Sub

    Private Sub CudaStoppedIcon() Handles cudaController.CudaStoppedNow
        appIcon.Icon = My.Resources.LogoGray
        mnuStartStop.Text = "Start"
    End Sub
#End Region

End Class
